package nl.utwente.eemcs.graph;

/**
 * Conditional constructs can be used to define conditional behaviour in a graph
 * @author Evert Duipmans
 */
public class ConditionalConstruct extends BranchedConstruct<ConditionalStartNode, ConditionalEndNode> {

	/**
	 * True branch of the conditional element
	 */
	private Graph trueBranch;
	
	/**
	 * False branch of the conditional element
	 */
	private Graph falseBranch;
	
	/**
	 * Initializes a conditional element, with a name
	 * @param name Name of the element
	 */
	public ConditionalConstruct(String name) {
		super(name);
	}
	
	/**
	 * Set a branch
	 * @param branch Sub graph 
	 * @param condition Condition of the branch (true or false)
	 */
	public void setBranch(Graph branch, boolean condition) {
		if (condition)
			trueBranch = branch;
		else
			falseBranch = branch;
		
		// Update branches list
		super.branches.clear();
		
		if (trueBranch != null)
		   super.branches.add(trueBranch);
		if(falseBranch != null)
		   super.branches.add(falseBranch);
	}
	
	/**
	 * Get a specific branch
	 * @param condition Condition of the branch
	 * @return Branch
	 */
	public Graph getBranch(boolean condition) {
		return condition ? trueBranch : falseBranch;
	}
}